package wqh.controller.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.GeoLocation;
import wqh.domain.XkMonitorGeo;
import wqh.domain.XkSensorAlarm;
import wqh.pojo.JStree;
import wqh.pojo.MapRet;
import wqh.service.GeoLocationService;
import wqh.service.XkMonitorGeoService;
import wqh.service.XkSensorAlarmService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by devts on 2017/1/3.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/geo_location")
public class GeoLocationJSON {

    private final GeoLocationService geoLocationService;

    @Autowired
    private XkSensorAlarmService xkSensorAlarmService;

    @Autowired
    private XkMonitorGeoService xkMonitorGeoService;

    @Autowired
    public GeoLocationJSON(GeoLocationService geoLocationService) {
        this.geoLocationService = geoLocationService;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody GeoLocation geoLocation){
        geoLocation.setId(null);
        geoLocation.setParent_id(geoLocation.getParent_id() == null ? 0L : geoLocation.getParent_id());
        geoLocation.setLevel(geoLocation.getLevel() == null ? 1 : geoLocation.getLevel());
        GeoLocation g = geoLocationService.findById(geoLocation.getParent_id());
        if(geoLocation.getParent_id() != 0 && g == null){
            return MapRet.ERROR("parent_id不存在");
        }
        else if(geoLocation.getParent_id() != 0){
            if(g.getLevel() == null || g.getLevel() == 3){
                return MapRet.ERROR("父节点level错误");
            }else{
                geoLocation.setLevel(g.getLevel() + 1);
            }
        }
        try {
            geoLocationService.save(geoLocation);
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_INSERT_ERROR();
        }
    }

    @RequestMapping(value = "/getTree",method = RequestMethod.GET)
    public Object getAll(){
//        System.out.println("最大级别："+geoLocationService.findMaxLevel());
        List<GeoLocation> geoLocationList = geoLocationService.findAll();
        if(geoLocationList.size() == 0) return MapRet.DATA(null);
        List<JStree> jStreeList = new ArrayList<JStree>();
        for(GeoLocation g : geoLocationList){
            if(g.getLevel() == 1){
                List<JStree> jStreeList1 = new ArrayList<JStree>();
                for(GeoLocation g2 : geoLocationList){
                    if(g2.getLevel() == 2 && g2.getParent_id().equals(g.getId())){
                        List<JStree> jStreeList2 = new ArrayList<JStree>();
                        for(GeoLocation g3 : geoLocationList){
                            if(g3.getLevel() == 3 && g3.getParent_id().equals(g2.getId())){
                                //需要更多的级别就扩展，这里现在只扩展到level3
                               jStreeList2.add(new JStree(g3.getId(),g3.getName(), 3,null));
                            }
                        }
                        jStreeList1.add(new JStree(g2.getId(),g2.getName(),2,jStreeList2));
                    }
                }
                jStreeList.add(new JStree(g.getId(),g.getName(),1,jStreeList1));
            }
        }
        return MapRet.DATA(jStreeList);
    }

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public Object getByParentID(@RequestParam(value = "id",defaultValue = "0") long id){
        GeoLocation g = geoLocationService.findById(id);
        if(g == null) return MapRet.DATA(null);
        g.setUrl(null);
        if(g.getLevel() <= 1){
            g.setPos_x(null);
            g.setPos_y(null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("alarm_status",false);
        List<XkSensorAlarm> xkSensorAlarmList = xkSensorAlarmService.findAll();
        if(xkSensorAlarmList.size() != 0){
            if(g.getLevel() == 1){
                List<GeoLocation> g2List = geoLocationService.findByParentID(g.getId());
                if(g2List.size() != 0){
                    for(GeoLocation g3 : g2List){
                        List<XkMonitorGeo> xkMonitorGeoList = xkMonitorGeoService.findByGeoId(g3.getId());
                        if(xkMonitorGeoList.size() != 0){
                            for(XkMonitorGeo monitor : xkMonitorGeoList){
                                for(XkSensorAlarm alarm : xkSensorAlarmList){
                                    if(alarm.getDevice_id().equals(monitor.getDevice().getDevice_id()) && alarm.getSensor_id().equals(monitor.getSensor_id())){
                                        map.put("alarm_status",true); break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if(g.getLevel() == 2){
                List<GeoLocation> g3List = geoLocationService.findByParentID(g.getId());
                if(g3List.size() != 0){
                    for(GeoLocation g3 : g3List){
                        List<XkMonitorGeo> xkMonitorGeoList = xkMonitorGeoService.findByGeoId(g3.getId());
                        if(xkMonitorGeoList.size() != 0){
                            for(XkMonitorGeo monitor : xkMonitorGeoList){
                                for(XkSensorAlarm alarm : xkSensorAlarmList){
                                    if(alarm.getDevice_id().equals(monitor.getDevice().getDevice_id()) && alarm.getSensor_id().equals(monitor.getSensor_id())){
                                        map.put("alarm_status",true); break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if(g.getLevel() == 3){
                List<XkMonitorGeo> xkMonitorGeoList = xkMonitorGeoService.findByGeoId(g.getId());
                if(xkMonitorGeoList.size() != 0){
                    for(XkMonitorGeo monitor : xkMonitorGeoList){
                        for(XkSensorAlarm alarm : xkSensorAlarmList){
                            if(alarm.getDevice_id().equals(monitor.getDevice().getDevice_id()) && alarm.getSensor_id().equals(monitor.getSensor_id())){
                                map.put("alarm_status",true); break;
                            }
                        }
                    }
                }
            }

        }
        map.put("root",g);
        map.put("children",new ArrayList<>());
        List<GeoLocation> gList = geoLocationService.findByParentID(id);
        if(gList.size() == 0){
            return MapRet.DATA(map);
        }else{
            for(GeoLocation gt : gList){
                gt.setUrl(null);
            }
            map.put("children",gList);
            return MapRet.DATA(map);
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody GeoLocation geoLocation){
        try {
            if(geoLocation.getName() == null || geoLocation.getId() == null)  return MapRet.SQL_UPDATE_ERROR();
            GeoLocation g = geoLocationService.findById(geoLocation.getId());
            g.setName(geoLocation.getName());
            geoLocationService.update(g);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/update_pos",method = RequestMethod.POST)
    public Object update_pos(@RequestBody GeoLocation geoLocation){
        try {
            if(geoLocation.getPos_x() == null
                    || geoLocation.getId() == null
                    || geoLocation.getPos_y() == null)  return MapRet.SQL_UPDATE_ERROR();
            GeoLocation g = geoLocationService.findById(geoLocation.getId());
            g.setPos_x(geoLocation.getPos_x());
            g.setPos_y(geoLocation.getPos_y());
            geoLocationService.update(g);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestBody GeoLocation geoLocation){
        try {
            GeoLocation g = geoLocationService.findById(geoLocation.getId());
            if(g == null) return MapRet.DATA("ID不存在");
            if(g.getLevel() == 3){
                geoLocationService.delete(new GeoLocation(g.getId(),true));
            }else if(g.getLevel() == 2){
                geoLocationService.delete(new GeoLocation(g.getId(),3));
                geoLocationService.delete(new GeoLocation(g.getId(),true));
            }else if(g.getLevel() == 1){
                List<GeoLocation> g2List = geoLocationService.findByParentID(g.getId());
                    if(g2List.size() != 0){
                        for(GeoLocation g2 : g2List){
                            geoLocationService.deleteByParentIdAndLevel(g2.getId(),3);
                        }
                        geoLocationService.deleteByParentIdAndLevel(g.getId(),2);
                    }
                    geoLocationService.delete(new GeoLocation(g.getId(),true));
            }

            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_DELETE_ERROR();
        }
    }

}
